package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduComment;
import com.zjitc.serviceedu.entity.EduTeacher;
import com.zjitc.serviceedu.entity.vo.CommentVo;
import com.zjitc.serviceedu.entity.vo.TeacherVo;
import com.zjitc.serviceedu.service.EduCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/serviceedu/edu-comment")
@Api(tags = "评论区")
public class EduCommentController {
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
    @Autowired
    private EduCommentService commentService;

    @PostMapping("addComment")
    @ApiOperation("评论区添加功能")
    public RespBean addComment(EduComment eduComment){
        boolean save = commentService.save(eduComment);
        if (save){
            return RespBean.success().msg("评论区添加成功");
        }
        return RespBean.error(201,"评论区添加失败");
    }
    @ApiOperation("评论区查询")
    @GetMapping("CommentList")
    public RespBean CommentList(EduComment eduComment) {
        List<EduComment> list = commentService.list(null);
        return RespBean.success().data("list",list).msg("查询成功");
    }
    @ApiOperation("分页查询")
    @GetMapping("getPage")
    public RespBean getPage(CommentVo commentVo) {
        Page<EduComment> page = new Page<>(commentVo.getPage(), commentVo.getSize());
        if(page == null) {
            throw new AuthException("查询失败");
        }
        List<EduComment> list = page.getRecords();
        Long total = page.getTotal();

        if (list.isEmpty()) {
            return RespBean.success().data("list", list).data("total", total).msg("未找到符合条件的课程");
        }

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");
    }
}

